package com.example.atc.myapplication;

import android.widget.Button;

import java.util.List;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Random;

import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by ATC on 2017/12/20.
 */

public class MainViewModel {

    Button btn_num1;
    Button btn_num2;
    Button btn_num3;
    Button btn_num4;
    Button btn_jia;
    Button btn_jian;
    Button btn_cheng;
    Button btn_chu;
    Button btn_M;
    Button btn_C;
    Button btn_help;
    Button btn_next;
    Button btn_display;

    ImageView img1;
    ImageView img2;
    ImageView img3;
    ImageView img4;

    public String displayOut = "";
    public String calString;
    public int leftNumber;
    public int rightNumber;
    public boolean prepareLeftNumber;
    public boolean prepareCal;
    public int answerIndex;
    public List<String> answer = new ArrayList<String>();

    private static int[][] cal_queue = {{0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {0, 3, 2, 1},
            {1, 0, 2, 3}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 2, 3, 0}, {1, 3, 0, 2}, {1, 3, 2, 0},
            {2, 1, 0, 3}, {2, 1, 3, 0}, {2, 0, 1, 3}, {2, 0, 3, 1}, {2, 3, 1, 0}, {2, 3, 0, 1},
            {3, 1, 2, 0}, {3, 1, 0, 2}, {3, 2, 1, 0}, {3, 2, 0, 1}, {3, 0, 1, 2}, {3, 0, 2, 1}};
    private static int[] resArray = {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, R.drawable.p8, R.drawable.p9, R.drawable.p10,
            R.drawable.p11, R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15, R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19, R.drawable.p20,
            R.drawable.p21, R.drawable.p22, R.drawable.p23, R.drawable.p24, R.drawable.p25, R.drawable.p26, R.drawable.p27, R.drawable.p28, R.drawable.p29, R.drawable.p30,
            R.drawable.p31, R.drawable.p32, R.drawable.p33, R.drawable.p34, R.drawable.p35, R.drawable.p36, R.drawable.p37, R.drawable.p38, R.drawable.p39, R.drawable.p40,
            R.drawable.p41, R.drawable.p42, R.drawable.p43, R.drawable.p44, R.drawable.p45, R.drawable.p46, R.drawable.p47, R.drawable.p48, R.drawable.p49, R.drawable.p50,
            R.drawable.p51, R.drawable.p52, R.drawable.p53, R.drawable.p54};

    private boolean cal24(int x1, int x2, int x3, int x4) {
        boolean result = false;
        int[] data = new int[]{x1, x2, x3, x4};

        List<Integer> isCal = new ArrayList<Integer>();
        for (int i = 0; i < 24; ++i) {
            int[] num = new int[]{data[cal_queue[i][0]], data[cal_queue[i][1]], data[cal_queue[i][2]], data[cal_queue[i][3]]};
            int total = num[0] + num[1] * 100 + num[2] * 10000 + num[3] * 1000000;
            if (!isCal.contains(total)) {
                isCal.add(total);
                result |= cal(num[0], num[1], num[2], num[3]);
            }
        }
        return result;
    }

    private boolean cal(int a, int b, int c, int d) {
        String[] cc = new String[]{"+", "-", "*", "/"};

        List<Integer> result1 = cal2num(cal2num(cal2num(a, b), c), d);
        List<Integer> result2 = cal2num(cal2num(a, b), cal2num(c, d));
        List<Integer> result3 = cal2num(cal2num(a, cal2num(b, c)), d);

        for (int i = 0; i < result1.size(); ++i) {
            if (result1.get(i) == 24) {
                answer.add(String.format("((%d %s %d) %s %d) %s %d = 24", a, cc[i / 16], b, cc[i % 16 / 4], c, cc[i % 16 % 4], d));
            }
        }

        for (int i = 0; i < result2.size(); ++i) {
            if (result2.get(i) == 24) {
                answer.add(String.format("(%d %s %d) %s (%d %s %d) = 24", a, cc[i / 16], b, cc[i % 16 % 4], c, cc[i % 16 / 4], d));
            }
        }

        for (int i = 0; i < result3.size(); ++i) {
            if (result3.get(i) == 24) {
                answer.add(String.format("(%d %s (%d %s %d)) %s %d = 24", a, cc[i % 16 / 4], b, cc[i / 16], c, cc[i % 16 % 4], d));
            }
        }
        return answer.size() > 0;
    }

    private List<Integer> cal2num(int x, int y) {
        List<Integer> result = new ArrayList<Integer>();
        if (x == -1 || y == -1) {
            result.add(-1);
            result.add(-1);
            result.add(-1);
            result.add(-1);
            return result;
        }
        result.add(x + y);
        result.add(x - y >= 0 ? x - y : -1);
        result.add(x * y);
        result.add(y != 0 && x % y == 0 ? x / y : -1);
        return result;
    }

    private List<Integer> cal2num(int x, List<Integer> y) {
        List<Integer> result = new ArrayList<Integer>();
        for (int yy : y) {
            for (int z : cal2num(x, yy)) {
                result.add(z);
            }
        }
        return result;
    }

    private List<Integer> cal2num(List<Integer> x, int y) {
        List<Integer> result = new ArrayList<Integer>();
        for (int xx : x) {
            for (int z : cal2num(xx, y)) {
                result.add(z);
            }
        }
        return result;
    }

    private List<Integer> cal2num(List<Integer> x, List<Integer> y) {
        List<Integer> result = new ArrayList<Integer>();
        for (int xx : x) {
            for (int yy : y) {
                for (int z : cal2num(xx, yy)) {
                    result.add(z);
                }
            }
        }
        return result;
    }

//    private List<Integer> cal2num(Object a, Object b)
//    {
//        List<Integer> result = new ArrayList<Integer>();
//        if( a == "")
//        if(a is int && b is int)
//        {
//            int x = (int)a;
//            int y = (int)b;
//            if(x == -1 || y == -1)
//            {
//                return new List<Integer>{-1,-1,-1,-1};
//            }
//            return new List<Integer>{x+y,x-y>=0?x-y:-1,x*y,y!=0 && x%y==0?x/y:-1};
//        }
//        if(a is int && b is List<Integer>)
//        {
//            int x= (int)a;
//            List<Integer> y = b as List<Integer>;
//            foreach(var yy in y)
//            {
//                foreach(var z in cal2num(x, yy))
//                {
//                    result.Add(z);
//                }
//            }
//        }
//        if(a is List<Integer> && b is List<Integer>)
//        {
//            List<Integer> x= a as List<Integer>;
//            List<Integer> y = b as List<Integer>;
//            foreach(var xx in x)
//            {
//                foreach(var yy in y)
//                {
//                    foreach(var z in cal2num(xx,yy))
//                    result.Add(z);
//                }
//            }
//        }
//        if(a is List<Integer> && b is int)
//        {
//            List<Integer> x= a as List<Integer>;
//            int y = (int)b;
//            foreach(var xx in x)
//            {
//                foreach(var z in cal2num(xx,y))
//                result.Add(z);
//            }
//        }
//        return result;
//    }

    public void createGame() {

        int num1, num2, num3, num4;
        int s_num1, s_num2, s_num3, s_num4;
        Random rd = new Random();

        do {
            num1 = rd.nextInt(54);
            num2 = rd.nextInt(54);
            num3 = rd.nextInt(54);
            num4 = rd.nextInt(54);

            s_num1 = switchNum(num1);
            s_num2 = switchNum(num2);
            s_num3 = switchNum(num3);
            s_num4 = switchNum(num4);
            answer.clear();
        } while (!cal24(s_num1, s_num2, s_num3, s_num4));

        img1.setImageResource(resArray[num1]);
        img2.setImageResource(resArray[num2]);
        img3.setImageResource(resArray[num3]);
        img4.setImageResource(resArray[num4]);

        btn_num1.setText(String.valueOf(s_num1));
        btn_num2.setText(String.valueOf(s_num2));
        btn_num3.setText(String.valueOf(s_num3));
        btn_num4.setText(String.valueOf(s_num4));
    }

    // 0-53 转化为1-10
    private int switchNum(int num) {
        if (num == 52 || num == 53 || num % 13 > 8) // 大小王10JQK
        {
            return 10;
        }
        return num % 13 + 1;
    }
}
